﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Hotel.Web.Models;

namespace Hotel.Web.Controllers
{
    public class AdministratorServiceController : Controller
    {
        [Authorize(Roles = "Administrators")]
        public ActionResult Index()
        {
            using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
            {
                List<Hotel.Module.Resources.Models.Service> DomainServices = m_ResourcesController.GetAllServices(Hotel.Module.Resources.Models.ServiceType.Rated);
                List<Service> serviceList = new List<Service>();
                foreach (var _domainService in DomainServices)
                {
                    serviceList.Add(Service.FromDomainModel(_domainService));
                }
                return View(serviceList);
            }
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Details(int id)
        {
            using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
            {
                Hotel.Module.Resources.Models.Service DomainService = m_ResourcesController.GetServiceByID(id);
                return View(Service.FromDomainModel(DomainService));
            }
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            Service _Service = new Service();
            return View(_Service);
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(Service Service)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
                    {
                        m_ResourcesController.CreateService(Service.Name, Service.Price, 0, true);
                        return RedirectToAction("Index");
                    }
                }
            }
            catch
            {
            }
            return View(Service);
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
            {
                Hotel.Module.Resources.Models.Service DomainService = m_ResourcesController.GetServiceByID(id);
                return View(Service.FromDomainModel(DomainService));
            }
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, Service Service)
        {
            try
            {
                // TODO: Add update logic here
                using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
                {
                    Hotel.Module.Resources.Models.Service ServiceUpdate = m_ResourcesController.GetServiceByID(id);
                    Service.State = true;
                    Service.AssignTo(ServiceUpdate);
                    m_ResourcesController.Storage.Update(ServiceUpdate); 
                    m_ResourcesController.Storage.SaveChanges();
                    //throw new NotSupportedException("Update method doesn't work for Service");
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
            {
                Hotel.Module.Resources.Models.Service DomainService = m_ResourcesController.GetServiceByID(id);
                return View(Service.FromDomainModel(DomainService));
            }
        }

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            using (Hotel.Module.Resources.HotelResourcesController m_ResourcesController = Hotel.Web.Helpers.ServiceLocator.Instance.GetService(typeof(Hotel.Module.Resources.IResourseManagment)) as Hotel.Module.Resources.HotelResourcesController)
            {
                Hotel.Module.Resources.Models.Service ServiceDelete = m_ResourcesController.GetServiceByID(id);
                try
                {
                    m_ResourcesController.Storage.Delete(ServiceDelete);
                    m_ResourcesController.Storage.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View(ServiceDelete);
                }
            }
        }

    }
}
